/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package intershopxssscanner.filters;

import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Willem
 */
public class RemoveIncludeLineFilter implements Filter {

    private String regex;
    private boolean removeLine = true;
    private int id;
    private String REMOVE_SETTING = "remove";

    public String doFilterLine(String input) {
        Pattern pattern = Pattern.compile(regex.trim());

        Matcher matcher = pattern.matcher(input);

        boolean found = false;
        while (matcher.find()) {
            found = true;
        }

        if ((removeLine && found) || (!removeLine && !found)) {
            return null;
        } else {
            return input;
        }
    }

    public ArrayList<String> doFilter(ArrayList<String> input) {
        ArrayList<String> output = new ArrayList<String>();

        for (String line : input) {
            line = doFilterLine(line);

            if (line == null) {
                continue;
            }
            else{
                output.add(line);
            }
        }

        return output;
    }

    @Override
    public void loadSettings(Properties prop, String id) {
        setRegex(prop.getProperty(Filter.PREFIX + id + Filter.DEL + Filter.REGEX));

        String setting = prop.getProperty(Filter.PREFIX + id + Filter.DEL + Filter.SETTINGS);

        if (setting.split(Filter.SETTINGS_DEL)[0].equalsIgnoreCase(REMOVE_SETTING)) {
            removeLine = true;
        } else {
            removeLine = false;
        }
    }

    /**
     * @return the regex
     */
    public String getRegex() {
        return regex;
    }

    /**
     * @param regex the regex to set
     */
    public void setRegex(String regex) {
        this.regex = regex;
    }

    /**
     * @return the remove
     */
    public boolean isRemove() {
        return removeLine;
    }

    /**
     * @param remove the remove to set
     */
    public void setRemove(boolean remove) {
        this.removeLine = remove;
    }

    public void setID(int id) {
        this.id = id;
    }

    @Override
    public int getID() {
        return this.id;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + "(" + getID() + ")";
    }
}
